#import "wooddoc.h"

@implementation WoodDoc (TargetAction)

- changeLayout:sender
{
	NXRect dummyRect;
	
    if([[NXApp pageLayout:self] runModal] == NX_OKTAG){
		[window disableFlushWindow];
		calcFrame(printInfo, &dummyRect);
		[treeView checkResize:&dummyRect for:tree];
		[self dirty:YES];
		[scrollView display];
		[window reenableFlushWindow];
		[window flushWindow];
		[window makeFirstResponder:treeView];
    }
    return self;
}

- save:sender
{
    id savepanel;
	const char	*dir = NULL;
	char *oldName = NULL;
	BOOL changeName = NO;	
	
	 if(theOOEClient != nil) {
	    [self updateOOETarget:sender];
	    [self clean:self];
	    return self;
    }
    savepanel = [SavePanel new];
	[savepanel setAccessoryView:nil];
	[savepanel setRequiredFileType:[[self class] docExtension]];
    if (![self hasSavedDocument]) {
		changeName = YES;
		if (![savepanel runModalForDirectory:directory file:name]) 
			return nil;
    }
	if(changeName){
		oldName = NXCopyStringBufferFromZone([self fileName],[self zone]);
		[self setName:[savepanel filename]];
	}
    if([self save]){
		[self dirty:NO];
		[self setSavedDocument:YES];
		dir = [self directory];
		[NXApp setDefaultDir:dir];
		return self;
	} else {
		if(changeName){
			[self setName:oldName];		
			NX_FREE(oldName);
		}
    	return nil;
	}
}

- saveAs:sender
{
	BOOL oldSavedFile, oldDirty;
	
	oldDirty = [self needsSaving];
	oldSavedFile = [self hasSavedDocument];
    [self dirty:YES];
    [self setSavedDocument:NO];
	if ([self save:sender]) 
		return self;
	else {
		[self setSavedDocument:oldSavedFile];
		[self dirty:oldDirty];
		return nil;
	}
}

- saveTo:sender
{	
	id savepanel;
	NXStream *stream;
	
    savepanel = [SavePanel new];
	if(!saveToAccessoryWindow)
		[NXApp loadNibSection:"SaveTo.nib" owner:self withNames:NO];
	[savepanel setAccessoryView:[saveToAccessoryWindow contentView]];
	[savepanel setTitle:"Save to"];
	[savepanel setRequiredFileType:"diagram2"];
	//if(![savepanel runModalForDirectory:NXHomeDirectory() file:"Untitled.diagram2"])
	if(![savepanel runModal]) 
		return nil;
	[printInfo setHorizontalPages:[treeView canvasX]];
	[printInfo setVerticalPages:[treeView canvasY]];	
	if([saveToAccessoryControl selectedCol] == 0)
		[Tree saveToD2Tree:tree fromView:treeView toFile:[savepanel filename] printInfo:printInfo];
	else {
		stream = NXOpenMemory(NULL,0,NX_WRITEONLY);
		[treeView writePSToStream:stream usingTree:tree]; 
		NXSaveToFile(stream, [savepanel filename]);
		NXCloseMemory(stream,NX_FREEBUFFER);
	}	
	[savepanel setTitle:"Save"];
	[savepanel setRequiredFileType:[[self class] docExtension]];
	[savepanel setAccessoryView:nil];
	return self;
}

- selectSaveToFormat:sender
{
	id savepanel;
	
	savepanel = [SavePanel new];
	if([saveToAccessoryControl selectedCol] == 0)
		[savepanel setRequiredFileType:"diagram2"];
	else
		[savepanel setRequiredFileType:"eps"];
	return self;
}
	
- revertToSaved:sender
{
	WoodDoc *dummy;
	
    if (![self hasSavedDocument] ||
		![self needsSaving] ||
		(NXRunAlertPanel(localString("Revert"),
						 localString("%s has been edited.  Are you sure you want to undo changes?"),
						 localString("Revert"),
						 localString("Cancel"), NULL, name) != NX_ALERTDEFAULT)) {
			return self;
    }
	dummy = self;
	[[self class] setIncPos:NO];
	self = [[[NXApp docClass] allocFromZone:[NXApp newDocZone]] initFromFile:[dummy fileName]];
	[[self class] setIncPos:YES];
	[[dummy window] close];
	return self;
}

- copy:sender;
{	
	return [self copyToPasteboard:[Pasteboard new]];
}


- cut:sender
{	
	if([self copyToPasteboard:[Pasteboard new]])
		return [self delete:self];
	return nil;
}

- paste:sender
{	
	return [self pasteFromPasteboard:[Pasteboard new]];
}

- delete:sender
{	
	if(!lastSelectedNode)
		return self;
	if(lastSelectedNode == tree){
		[undoManager freeUndoArgsOnRecordDiscard];
		[undoManager setUndoName:"Deletion"];
		[undoManager setRedoName:"Addition"];
		[[undoManager setUndoTarget:self] makeAddTree:tree];
		tree = lastSelectedNode = nil;
		[self setEmpty:YES];
		[self updateViewsDirty:YES rect:NULL flag:UPDATE_ALL];
	} else
		[[lastSelectedNode parent] removeChild:lastSelectedNode];
	[[NXApp inspectorManager] revert:self];
	return self;
}

- add:sender
{
	[self makeAddTree:[[Tree allocFromZone:[self zone]] initLabel:NULL props:&defaultProps]];
	return self;
}

- addFromFilter:sender
{
	id filter;
	
	filter = [NXApp filter:[[sender selectedCell] tag]];
	if([filter conformsTo:@protocol(PFilter)]){
		[self makeAddTree:[[filter class] filterWithProps:&defaultProps inZone:[self zone]]];
	} 
	return self;
}

- print:sender
{
	[treeView printPSCode:nil];
	return self;
}

- copyStyle:sender;
{	
	return [self copyStyleToPasteboard:[Pasteboard new]];
}

- pasteStyle:sender
{	
	return [self pasteStyleFromPasteboard:[Pasteboard new]];
}

- promoteStyle:sender;
{
	if(!lastSelectedNode)
		return nil;
	[lastSelectedNode applyStyleToSubtree];
	return self;
}

- toggleMarker:sender
{
	showMarker = !showMarker;
	[self updateViewsDirty:NO rect:NULL flag:UPDATE_TREEVIEW];
	return self;	
}	

- changeFont:sender
{
	if(!lastSelectedNode)
		return nil;
	[lastSelectedNode changeFont:sender];
	return self;
}

- changeZip:sender
{
	if(!lastSelectedNode)
		return nil;
	[lastSelectedNode changeZipped];
	return self;
}

- changeDescription:sender
{
	if(!lastSelectedNode)
		return nil;
	[lastSelectedNode changeDescriptionFrom:[textView docView]];
	textViewSaved = YES;
	return self;
}

- undo:sender
{
	[undoManager undo:sender];
	return self;
}

- redo:sender
{
	[undoManager redo:sender];
	return self;
}

- clean:sender
{
    [window setDocEdited:NO];
    return self;
}

- close:sender
{
    [window performClose:self];
    return self;
}

@end
